import axios from '@/utils/axios'

// 列匹配模式
export type ColumnMatchMode = 'BY_INDEX' | 'BY_NAME'

// 行状态
export type RowStatus = 'SAME' | 'MODIFIED' | 'ADDED' | 'DELETED'

// 对比配置
export interface ComparisonConfig {
  ignoreRowOrder?: boolean
  columnMatchMode?: ColumnMatchMode
  ignoreCase?: boolean
  ignoreSpace?: boolean
}

// 表格信息
export interface TableInfo {
  totalRows: number
  totalColumns: number
  fileName: string
}

// 对比统计
export interface ComparisonStatistics {
  sameRows: number
  modifiedRows: number
  addedRows: number
  deletedRows: number
  totalDifferences: number
}

// 对比摘要
export interface ComparisonSummary {
  table1: TableInfo
  table2: TableInfo
  comparison: ComparisonStatistics
}

// 列映射
export interface ColumnMapping {
  index: number
  table1Name: string
  table2Name: string
  matched: boolean
}

// 单元格差异
export interface CellDifference {
  columnIndex: number
  columnName: string
  table1Value: string
  table2Value: string
}

// 行对比
export interface RowComparison {
  rowNumber: number
  status: RowStatus
  table1Data: string[] | null
  table2Data: string[] | null
  differences: CellDifference[]
}

// 对比详情
export interface ComparisonDetails {
  columns: ColumnMapping[]
  rows: RowComparison[]
}

// 对比结果
export interface ComparisonResult {
  comparisonId: string
  summary: ComparisonSummary
  details: ComparisonDetails
}

// 对比请求参数
export interface CompareTableParams {
  table1Url: string
  table2Url: string
  config?: ComparisonConfig
}

// 表格对比接口
export const compareTable = async (params: CompareTableParams): Promise<ComparisonResult> => {
  const response = await axios.post('/api/utility/table-comparison/compare', params)
  if (response.code === 200) {
    return response.data
  }
  throw new Error(response.message || '对比失败')
}

// 下载对比结果
export const downloadComparisonResult = async (comparisonId: string): Promise<Blob> => {
  const response = await axios.get(`/api/utility/table-comparison/download/${comparisonId}`, {
    responseType: 'blob'
  })
  return response.data
}